<template>
    <div>
        <el-card style="margin-bottom: 20px">
        <el-breadcrumb  separator-class="el-icon-arrow-right" style="margin-bottom: 20px" >
            <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>志愿者</el-breadcrumb-item>
            <el-breadcrumb-item>志愿者管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入"  v-model="search"
        v-on:keyup.enter.native="text">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="addDialogVisible = true">添加用户</el-button>
        </el-col>
      </el-row>
    </el-card>

    <el-card>
      <el-table :data="tableData">
        <el-table-column label="创建时间" prop="createTime"> </el-table-column>
        <el-table-column label="招聘标题" prop="total"> </el-table-column>
        <el-table-column label="职位" prop="position" ></el-table-column>
        <el-table-column label="工资待遇" prop="work_salary"></el-table-column>
        <el-table-column label="招聘要求" prop="work_require"></el-table-column>
        <el-table-column label="工作地点" prop="vwork_place"></el-table-column>
        <el-table-column label="工作时间" prop="work_time"></el-table-column>
        <el-table-column label="招聘人数" prop="recruits_num"></el-table-column>
        <el-table-column label="招聘时间" prop="recruits_time"></el-table-column>
        <el-table-column label="截止时间" prop="recruits_end_time"></el-table-column>
        <el-table-column label="负责人" prop="lead"></el-table-column>
        <el-table-column label="联系方式" prop="contact"></el-table-column>
        <el-table-column label="发布日期" prop="release_date"></el-table-column>
        <el-table-column label="最近点击时间" prop="last_click_time"></el-table-column>
        <el-table-column label="点击次数" prop="click_num"></el-table-column>
        <el-table-column label="图片" prop="picture">
          <template slot-scope="scope">
            <img width="100px" :src="scope.row.picture" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="handleDel(scope.$index, scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
       <el-pagination
          @current-change="current"
          layout="prev, pager, next"
          :page-size="pageSize"
          background
          :total="total"
        >
        </el-pagination>
    </div>
    </el-card>

    <!-- 志愿者添加 -->
    <el-dialog title="志愿者添加" :visible.sync="addDialogVisible" width="50%">
      <el-form :model="addForm">
        <el-form-item label="创建时间">
          <el-input v-model="addForm.createTime"></el-input>
        </el-form-item>
        <el-form-item label="招聘标题">
          <el-input v-model="addForm.total"></el-input>
        </el-form-item>
        <el-form-item label="职位">
          <el-input v-model="addForm.position"></el-input>
        </el-form-item>
        <el-form-item label="招聘要求">
          <el-input v-model="addForm.work_require"></el-input>
        </el-form-item>
        <el-form-item label="工资待遇">
          <el-input v-model="addForm.work_salary"></el-input>
        </el-form-item>
        <el-form-item label="工作地点">
          <el-input v-model="addForm.work_place"></el-input>
        </el-form-item>
        <el-form-item label="工作时间">
          <el-input v-model="addForm.work_time"></el-input>
        </el-form-item>
        <el-form-item label="招聘人数">
          <el-input v-model="addForm.recruits_num"></el-input>
        </el-form-item>
        <el-form-item label="招聘时间">
          <el-input v-model="addForm.recruits_time"></el-input>
        </el-form-item>
        <el-form-item label="截止时间">
          <el-input v-model="addForm.recruits_end_time"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
            class="upload-demo"
            action="action"
            :auto-upload="true"
            :limit="1"
            ref="upload"
            accept="image/jpeg,image/png,image/jpg"
            :http-request="uploadFile"
            multiple
          >
            <el-button type="primary" size="small">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传图片</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model="addForm.lead"></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="addForm.contact"></el-input>
        </el-form-item>
        <el-form-item label="发布日期">
          <el-input v-model="addForm.release_date"></el-input>
        </el-form-item>
        <el-form-item label="最近点击时间">
          <el-input v-model="addForm.last_click_time"></el-input>
        </el-form-item>
        <el-form-item label="点击次数">
          <el-input v-model="addForm.click_num"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addUser">提交</el-button>
      </span>
    </el-dialog>

    <!-- 志愿者编辑 -->
    <el-dialog title="志愿者编辑" :visible.sync="editDialogVisible" width="50%">
      <el-form :model="editForm">
        <el-form-item label="创建时间">
          <el-input v-model="editForm.createTime"></el-input>
        </el-form-item>
        <el-form-item label="招聘标题">
          <el-input v-model="editForm.total"></el-input>
        </el-form-item>
        <el-form-item label="职位">
          <el-input v-model="editForm.position"></el-input>
        </el-form-item>
        <el-form-item label="招聘要求">
          <el-input v-model="editForm.work_require"></el-input>
        </el-form-item>
        <el-form-item label="工资待遇">
          <el-input v-model="editForm.work_salary"></el-input>
        </el-form-item>
        <el-form-item label="工作地点">
          <el-input v-model="editForm.work_place"></el-input>
        </el-form-item>
        <el-form-item label="工作时间">
          <el-input v-model="editForm.work_time"></el-input>
        </el-form-item>
        <el-form-item label="招聘人数">
          <el-input v-model="editForm.recruits_num"></el-input>
        </el-form-item>
        <el-form-item label="招聘时间">
          <el-input v-model="editForm.recruits_time"></el-input>
        </el-form-item>
        <el-form-item label="截止时间">
          <el-input v-model="editForm.recruits_end_time"></el-input>
        </el-form-item>
        <el-form-item label="图片:">
          <el-input  v-model="editForm.picture"></el-input>
        </el-form-item>
        <el-input type="hidden" v-model="editForm.picture"></el-input>
        <el-form-item label="负责人">
          <el-input v-model="editForm.lead"></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="editForm.contact"></el-input>
        </el-form-item>
        <el-form-item label="发布日期">
          <el-input v-model="editForm.release_date"></el-input>
        </el-form-item>
        <el-form-item label="最近点击时间">
          <el-input v-model="editForm.last_click_time"></el-input>
        </el-form-item>
        <el-form-item label="点击次数">
          <el-input v-model="editForm.click_num"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="editUser">提交</el-button>
      </span>
    </el-dialog>

    </div>
</template>

<script>
export default {
  data() {
    return {
      total: 3,
      search:'',
      editDialogVisible: false,
      addDialogVisible: false,
      addForm: {},
      editForm: {},
      tableData: [],
      pageNum: 1,
      pageSize: 3,
      nowImg: "",
    }
  },
  methods: {
    async getList(pageNum, pageSize) {
      //  let { data: res } = await this.$axios.get('data/fupin.json')
      //     window.console.log(res)
      //     this.tableData = res.data.list;
      let { data: res } = await this.$axios.get("/volunteer/getlist", {
        params: {
          pageSize,
          pageNum,
        },
      });
      window.console.log(res)
      this.total = res.data.total
      this.tableData = res.data.list
    },
    async uploadFile() {
        const formData = new FormData();
        const file = this.$refs.upload.uploadFiles[0];
        const headerConfig = { headers: { 'Content-Type': 'multipart/form-data' } };
        if (!file) { // 若未选择文件
          alert('请选择文件');
          return;
        }
        window.console.log('图片解析为：  '+file.raw)
        formData.append('file', file.raw);
        formData.append('type', 'YONGHU');
        let {data: res} =  await this.$axios.post('/uploadImg', formData, headerConfig)
        window.console.log(res)
        this.nowImg = res.data.imgurl
    },
    async addUser() {
      this.addForm.picture = this.nowImg;
      let { data: res } = await this.$axios.post("/volunteer/add", JSON.stringify(this.addForm),{
        headers:{
          "content-type":"application/json"
        }
      })
      if (res.code != 200) return this.$message.error(res.message);
      this.$message.success(res.code);
      this.getList(this.pageNum, this.pageSize);
      this.addDialogVisible = false;
    },
    async editUser() {
      let { data: res } = await this.$axios.post(
        "/volunteer/update",
        this.editForm
      );
      if (res.code != 200) return this.$message.error(res.message);
      this.$message.success(res.code);
      this.getList(this.pageNum, this.pageSize);
      this.editDialogVisible = false;
    },
    async handleEdit(index, row) {
      let { data: res } = await this.$axios.get("/volunteer/getlist", {
        params: {
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          id:row.id
        },
      });
      this.editForm = res.data.list[0];
      this.editDialogVisible = true;
    },
    async handleDel(index, id) {
      //window.console.log(index+"  "+id)
      let { data: res } = await this.$axios.get("/volunteer/delete", {
        params: {
          id: id,
        },
      });
      if (res.code != 200) return this.$message.error(res.message);
      this.getList(this.pageNum, this.pageSize);
    },
    current(e) {
      this.pageNum = e;
      this.getList(e, this.pageSize);
    },
  },
  created() {
    this.getList(this.pageNum, this.pageSize);
  },
};
</script>
